﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Library.AppWeb;
using MPR.Poker.DALEntity.SYS;
using MPR.Poker.Model.LIS;

namespace MPR.Poker.DALEntity.LIS
{
    public class DAL_Country
    {
        public DAL_Country()
        {
        }

        #region Insert/Update/Delete
        /// <summary>
        /// kiem tra country ton tai
        /// </summary>
        /// <param name="objCountry"></param>
        /// <returns></returns>
        public ResultMessage CheckExist(LIS_Country objCountry)
        {
            ResultMessage resultMess = new ResultMessage();
            LIS_Country Country = new LIS_Country();
            try
            {
                using (PokerLife db = new PokerLife())
                {
                    Country = db.LIS_Countrys.FirstOrDefault(i => i.IsDeleted == false & i.CountryID == objCountry.CountryID & i.IDSys != objCountry.IDSys);

                    if (Country != null && Country.CountryName.Trim().Length > 0)
                    {
                        resultMess = new ResultMessage(true, ResultMessage.ErrorTypes.CheckData, "Country này đã tồn tại", string.Empty);
                    }
                }
            }
            catch (Exception ex)
            {
                resultMess = new ResultMessage(true, ResultMessage.ErrorTypes.CheckData, "Lỗi tồn tại country", ex.InnerException != null ? ex.InnerException != null ? ex.InnerException.Message.ToString() : ex.Message.ToString() : ex.Message.ToString());
                DAL_Error.Insert(resultMess, 0, "LIS_Country --> CheckExist");
                return resultMess;
            }
            return resultMess;
        }

        /// <summary>
        /// them country chưa kiểm tra tồn tại hay chưa
        /// </summary>
        /// <param name="objCountry"></param>
        /// <returns></returns>
        public ResultMessage InsertNoCheck(LIS_Country objCountry)
        {
            ResultMessage resultMessage = new ResultMessage();
            LIS_Country Country = new LIS_Country();
            try
            {
                using (PokerLife db = new PokerLife())
                {
                    Country.CountryID = objCountry.CountryID;
                    Country.CountryName = objCountry.CountryName;
                    Country.AbbreviationName = objCountry.AbbreviationName;
                    Country.Note = objCountry.Note;
                    Country.OrderIndex = db.LIS_Countrys.Count() + 1;

                    db.LIS_Countrys.Add(Country);
                    db.SaveChanges();
                }
                objCountry.IDSys = Country.IDSys;
                objCountry.CountryName = Country.CountryName;
            }
            catch (Exception ex)
            {
                resultMessage = new ResultMessage(true, ResultMessage.ErrorTypes.Insert, "Lỗi thêm country", ex.InnerException != null ? ex.InnerException != null ? ex.InnerException.Message.ToString() : ex.Message.ToString() : ex.Message.ToString());
                DAL_Error.Insert(resultMessage, 0, "LIS_Country --> Insert");
                return resultMessage;
            }
            return new ResultMessage(false, ResultMessage.ErrorTypes.No_Error, "Thêm country thành công", string.Empty);
        }

        /// <summary>
        /// Them country có kiểm tra tồn tại
        /// </summary>
        /// <param name="objCountry"></param>
        /// <returns></returns>
        public ResultMessage Insert(LIS_Country objCountry)
        {
            ResultMessage objResultMessage = new ResultMessage();
            try
            {
                objResultMessage = CheckExist(objCountry);
                if (objResultMessage.IsError) return objResultMessage;

                objResultMessage = InsertNoCheck(objCountry);

            }
            catch (Exception objEx)
            {
                objResultMessage = new ResultMessage(true, ResultMessage.ErrorTypes.Insert, "Lỗi thêm country", objEx);
                DAL_Error.Insert(objResultMessage, 0, "LIS_Country --> Insert");
                return objResultMessage;
            }

            return new ResultMessage(false, ResultMessage.ErrorTypes.No_Error, "Thêm country thành công", string.Empty);
        }

        /// <summary>
        /// Check tồn tại country mới cập nhật
        /// </summary>
        /// <param name="objCountry"></param>
        /// <returns></returns>
        public ResultMessage UpdateNoCheck(LIS_Country objCountry)
        {
            ResultMessage objResultMessage = new ResultMessage();
            try
            {
                using (PokerLife db = new PokerLife())
                {
                   
                    LIS_Country Country = db.LIS_Countrys.FirstOrDefault(i => i.IDSys == objCountry.IDSys);
                    Country.CountryID = objCountry.CountryID;
                    Country.CountryName = objCountry.CountryName;
                    Country.AbbreviationName = objCountry.AbbreviationName;
                    Country.Note = objCountry.Note;

                    db.SaveChanges();

                }
            }
            catch (Exception objEx)
            {
                objResultMessage = new ResultMessage(true, ResultMessage.ErrorTypes.Update, "Lỗi cập nhật country", objEx);
                DAL_Error.Insert(objResultMessage, 0, "LIS_Country --> Update");
                return objResultMessage;
            }

            return new ResultMessage(false, ResultMessage.ErrorTypes.No_Error, "Cập nhật country thành công", string.Empty);
        }

        /// <summary>
        /// Update country
        /// </summary>
        /// <param name="objUser"></param>
        /// <returns></returns>
        public ResultMessage Update(LIS_Country objCountry)
        {
            ResultMessage objResultMessage = new ResultMessage();
            try
            {
                objResultMessage = CheckExist(objCountry);
                if (objResultMessage.IsError) return objResultMessage;

                objResultMessage = UpdateNoCheck(objCountry);
            }
            catch (Exception objEx)
            {
                objResultMessage = new ResultMessage(true, ResultMessage.ErrorTypes.Insert, "Lỗi cập nhật country", objEx);
                DAL_Error.Insert(objResultMessage, 0, "LIS_Country --> Update");
                return objResultMessage;
            }

            return new ResultMessage(false, ResultMessage.ErrorTypes.No_Error, "Cập nhật country thành công", string.Empty);
        }

        public ResultMessage Delete(LIS_Country objCountry)
        {
            ResultMessage objResultMessage = new ResultMessage();
            objResultMessage = CheckRelationship(objCountry);
            if (objResultMessage.IsError) return objResultMessage;

            objResultMessage = DeleteNoCheck(objCountry);

            return objResultMessage;
        }

        /// <summary>
        /// Xóa Quốc gia
        /// </summary>
        /// <param name="intUserIDSys">Mã Quốc gia</param>
        /// <param name="intDeletedUser">Mã Quốc gia</param>
        /// <returns></returns>
        public ResultMessage Delete(int intUserIDSys, int intDeletedUser)
        {
            ResultMessage objResultMessage = new ResultMessage();
            LIS_Country objCountry = new LIS_Country();
            objCountry.IDSys = intUserIDSys;

            objResultMessage = CheckRelationship(objCountry);
            if (objResultMessage.IsError) return objResultMessage;

            objResultMessage = DeleteNoCheck(objCountry);

            return objResultMessage;
        }

        public ResultMessage Delete(List<int> lstUserIDSys, int intDeletedUser)
        {
            ResultMessage objResultMessage = new ResultMessage();

            foreach (int IDSys in lstUserIDSys)
            {
                LIS_Country objCountry = new LIS_Country();
                objCountry.IDSys = IDSys;

                objResultMessage = CheckRelationship(objCountry);
                if (objResultMessage.IsError) return objResultMessage;

                objResultMessage = DeleteNoCheck(objCountry);
                if (objResultMessage.IsError) return objResultMessage;
            }

            return objResultMessage;
        }

        public ResultMessage CheckRelationship(LIS_Country objCountry)
        {
            ResultMessage objResultMessage = new ResultMessage();
            try
            {
                //using (PokerLife db = new PokerLife())
                //{
                //    SYS_User user = db.SYS_Users.FirstOrDefault(i => i.UserGroupIDSys == objUserGroup.IDSys);

                //    if (user != null && user.UID.Trim().Length > 0)
                //    {
                //        objResultMessage = new ResultMessage(true, ResultMessage.ErrorTypes.No_Error, "Có dữ liệu liên quan đến nhóm Quốc gia", string.Empty);
                //    }
                //}
            }
            catch (Exception objEx)
            {
                objResultMessage = new ResultMessage(true, ResultMessage.ErrorTypes.CheckData, "Lỗi kiểm tra dữ liệu liên quan Quốc gia", objEx);
                DAL_Error.Insert(objResultMessage, 0, "LIS_Country --> CheckRelationship");
                return objResultMessage;
            }
            return objResultMessage;
        }

        /// <summary>
        /// Xoa country không cần kiểm tra
        /// </summary>
        /// <param name="objCountry"></param>
        /// <returns></returns>
        public ResultMessage DeleteNoCheck(LIS_Country objCountry)
        {
            ResultMessage resultMessage = new ResultMessage();
            try
            {
                using (PokerLife db = new PokerLife())
                {
                    LIS_Country Country = db.LIS_Countrys.FirstOrDefault(i => i.IDSys == objCountry.IDSys);
                    if (Country == null || Country.IDSys == 0)
                    {
                        return resultMessage;
                    }
                    else
                    {
                        Country.IsDeleted = true;
                        Country.DeletedDate = DateTime.Now;

                        db.SaveChanges();
                    }
                }
            }
            catch (Exception objEx)
            {
                resultMessage = new ResultMessage(true, ResultMessage.ErrorTypes.Insert, "Lỗi xoá country", objEx);
                DAL_Error.Insert(resultMessage, 0, "LIS_Country --> Delete");
                return resultMessage;
            }

            return new ResultMessage(false, ResultMessage.ErrorTypes.No_Error, "Xoá country thành công", string.Empty);

        }
        #endregion

        #region Load
        /// <summary>
        /// Lấy thông tin chi tiết của country
        /// </summary>
        /// <param name="objCountry"></param>
        /// <param name="intCounytyIds"></param>
        /// <returns></returns>

        public ResultMessage LoadInfo(ref LIS_Country objCountry, int intCounytyIds)
        {
            ResultMessage objResultMessage = new ResultMessage();
            try
            {
                using (PokerLife db = new PokerLife())
                {
                    var Country = from gu in db.LIS_Countrys
                               where gu.IDSys == intCounytyIds
                               select gu;
                    objCountry = new LIS_Country();
                    foreach (var obj in Country)
                    {
                        if (!Convert.IsDBNull(obj.IDSys)) objCountry.IDSys = obj.IDSys;
                        if (!string.IsNullOrEmpty(obj.CountryID)) objCountry.CountryID = obj.CountryID;
                        if (!string.IsNullOrEmpty(obj.CountryName)) objCountry.CountryName = obj.CountryName;
                        if (!string.IsNullOrEmpty(obj.AbbreviationName)) objCountry.AbbreviationName = obj.AbbreviationName; 
                        if (!string.IsNullOrEmpty(obj.Note)) objCountry.Note = obj.Note;             
                        if (!Convert.IsDBNull(obj.OrderIndex)) objCountry.OrderIndex = obj.OrderIndex;
                        if (!Convert.IsDBNull(obj.IsDeleted)) objCountry.IsDeleted = obj.IsDeleted;
                        if (!Convert.IsDBNull(obj.DeletedDate)) objCountry.DeletedDate = obj.DeletedDate;
                    }
                }

            }
            catch (Exception objEx)
            {
                objResultMessage = new ResultMessage(true, ResultMessage.ErrorTypes.LoadInfo, "Lấy thông tin chi tiết country", objEx);
                DAL_Error.Insert(objResultMessage, 0, "LIS_Country --> LoadInfo");
                return objResultMessage;
            }
            return new ResultMessage(false, ResultMessage.ErrorTypes.No_Error, "Lấy thông tin chi tiết country thành công", string.Empty);
        }

        /// <summary>
        /// Load danh sách country
        /// </summary>
        /// <param name="dtCountry"></param>
        /// <returns></returns>
        public ResultMessage SearchData(ref DataTable dtCountry)
        {
            ResultMessage objResultMessage = new ResultMessage();
            dtCountry = new DataTable();

            try
            {
                using (PokerLife db = new PokerLife())
                {
                    dtCountry = ConvertData.ConvertToDataTable<LIS_Country>(db.LIS_Countrys.Where(i => i.IsDeleted == false).ToList());
                }
            }
            catch (Exception objEx)
            {
                objResultMessage = new ResultMessage(true, ResultMessage.ErrorTypes.SearchData, "Lấy danh sách country", objEx);
                DAL_Error.Insert(objResultMessage, 0, "LIS_Country --> SearchData");
                return objResultMessage;
            }

            return new ResultMessage(false, ResultMessage.ErrorTypes.No_Error, "Lấy danh sách country thành công", string.Empty);
        }

        /// <summary>
        /// Lấy danh sách country dạng list
        /// </summary>
        /// <param name="lstUser"></param>
        /// <returns></returns>
        public ResultMessage SelectToList(ref List<LIS_Country> lstCountry)
        {
            ResultMessage objResultMessage = new ResultMessage();
            try
            {
                using (PokerLife db = new PokerLife())
                {
                    var Country = from gu in db.LIS_Countrys
                               where gu.IsDeleted == false
                               orderby gu.OrderIndex
                               select gu;
                    lstCountry = Country.ToList();
                }
            }
            catch (Exception objEx)
            {
                objResultMessage = new ResultMessage(true, ResultMessage.ErrorTypes.SelectData, "Lấy thông tin dạng list country", objEx);
                DAL_Error.Insert(objResultMessage, 0, "LIS_Country --> SelectToList");
                return objResultMessage;
            }

            return new ResultMessage(false, ResultMessage.ErrorTypes.No_Error, "Lấy danh sách country dạng list thành công", string.Empty);
        }

        /// <summary>
        /// Load cache
        /// </summary>
        /// <param name="lstCountry"></param>
        /// <returns></returns>
        
        public ResultMessage SelectToCache(ref List<LIS_Country> lstCountry)
        {
            ResultMessage objResultMessage = new ResultMessage();
            try
            {
                using (PokerLife db = new PokerLife())
                {

                    IEnumerable<LIS_Country> Country = (from gu in db.LIS_Countrys
                                                  where gu.IsDeleted == false
                                                  orderby gu.OrderIndex
                                                  select new
                                                  {
                                                      IDSys = gu.IDSys,
                                                      CountryID = gu.CountryID,
                                                      CountryName = gu.CountryName,
                                                      gu.AbbreviationName,
                                                      Note = gu.Note,
                                                      OrderIndex = (Nullable<int>)gu.OrderIndex == null ? 0 : gu.OrderIndex
                                                  }).ToList().Select(c => new LIS_Country()
                                                  {
                                                      IDSys = c.IDSys,
                                                      CountryID = c.CountryID,
                                                      CountryName = c.CountryName,
                                                      AbbreviationName=c.AbbreviationName,
                                                      Note = c.Note,
                                                      OrderIndex = (Nullable<int>)c.OrderIndex == null ? 0 : 0
                                                  });

                    lstCountry = Country.ToList();
                }
            }
            catch (Exception objEx)
            {
                objResultMessage = new ResultMessage(true, ResultMessage.ErrorTypes.SelectData, "Lấy Cache danh sách country", objEx);
                DAL_Error.Insert(objResultMessage, 0, "LIS_Country --> SelectToListCache");
                return objResultMessage;
            }

            return new ResultMessage(false, ResultMessage.ErrorTypes.No_Error, "Lấy Cache danh sách country thành công", string.Empty);
        }

        /// <summary>
        /// Lay ds country - phan trang
        /// </summary>
        /// <param name="lstCountry"> Đối tượng country</param>
        /// <param name="intPageSize"> số record trên 1 trang</param>
        /// <param name="intPageNumber">lấy trang số mấy</param>
        /// <returns></returns>
        public ResultMessage SelectToList(ref List<LIS_Country> lstCountry, int intPageSize, int intPageNumber)
        {
            ResultMessage objResultMessage = new ResultMessage();
            try
            {
                using (PokerLife db = new PokerLife())
                {
                    var Country = from gu in db.LIS_Countrys
                               where gu.IsDeleted == false
                               orderby gu.OrderIndex
                               select gu;

                    lstCountry = Country.Skip((intPageNumber - 1) * intPageSize).Take(intPageSize).ToList();
                }
            }
            catch (Exception objEx)
            {
                objResultMessage = new ResultMessage(true, ResultMessage.ErrorTypes.SelectData, "Lấy thông tin country", objEx);
                DAL_Error.Insert(objResultMessage, 0, "LIS_Country --> SelectToList");
                return objResultMessage;
            }

            return new ResultMessage(false, ResultMessage.ErrorTypes.No_Error, "Lấy danh sách country thành công", string.Empty);
        }

        #endregion
    }
   
}
